<?php
require_once 'model/user.php';
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="public/css.css">
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <title></title>
    </head>
    <?php if (isset($_GET["uid"])): ?>
        <a href="index.php" style="font-size: 18px"><b>Log out</b></a>
    <?php endif; ?>
        <div id="bg"><img width="100%" height="100%" src="public/images/untitled.png" /></div>  


    <div class="image">
        <div class="rong">&nbsp;</div>
        <div id="menu">
            <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="#">Content</a></li>
                <li><a href="#">Forum</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div></br> 
        <div id="ten"> 
            <h2> <p></p><h2>
                    </div>
                    </div>  
                    <div class="xuong"></div> </br>
                    <?php
                    if (isset($_POST['submit'])) {
                        $username = $_POST['username'];
                        $password = $_POST['password'];
                        $Ok = FALSE;
                        $index = -1;
                        for ($i = 0; $i < count($users); $i++) {
                            if ((strcmp($users[$i]['username'], $username) == 0) &&
                                    (strcmp($users[$i]['password'], $password) == 0)) {
                                $Ok = TRUE;
                                $index = $i;
                            }
                        }
                        if ($Ok) {
                            header("Location: http://localhost:82/hngocthach-web2/index.php?uid=" . $users[$index]['id']);
                        } else {
                            echo "đăng nhập lại đi bạn :D";
                        }
                    }
                    ?>
                    <div class="login">   <table>
                            <form method="Post">
                                <tr>
                                    <td>Username: </td>
                                    <td><input type="text" name="username" id="name"></td>
                                </tr>
                                <tr>
                                    <td>Password: </td>
                                    <td><input type="password" name="password" id="password"></td>
                                </tr>
                                <tr>      <td>  <input type="submit" name="submit" value="Đăng Nhập"></td></tr>
                            </form>
                        </table>
                    </div> 
                    <?php
                    require_once 'model/db.php';
                    require_once 'model/works.php';
                    require_once 'model/pagination.php';
                    $works = new works();
                    $paginations = new Pagination();
                    ?>
                    <?php
                    if (!isset($_GET["page"]))
                        $page = 1;
                    else
                        $page = $_GET["page"];
                    $list_of_works = $works->select($page);
                    $total = $works->count();
                    echo $paginations->Display($page, $total, null, "index.php");
                    ?>
                    <div class="ajax_show">
                        
                    </div>
                    <br>
                   
                    <div class="table">
                        <table border="1px">
                            <tr>
                                <td>STT </td>
                                <td>Hình Ảnh </td>
                                <td>Tên </td>
                                <td>Nội Dung</td>
                                <?php
                                if (isset($_GET['uid'])) :
                                    if ($_GET['uid'] == 0) :
                                        ?>
                                        <td>Edit</td>
                                    <?php else : ?>
                                        <td>Edit</td>
                                    <?php
                                    endif;
                                endif;
                                ?>

                            </tr>
                            <?php foreach ($list_of_works as $list1): ?>
                                <tr>
                                    <td><?php echo $list1["id"]; ?></td>
                                    <td><img src="<?php echo $list1["image"]; ?>" width="100"></td>
                                    <td><?php echo $list1["name"]; ?></td>

                                    <td><?php echo substr($list1["description"], 0, 70); ?></td>
                                    <?php
                                    if (isset($_GET['uid'])) :
                                        if ($_GET['uid'] == 0) :
                                            ?>
                                            <td><a href="edit.php?id=<?php echo $list1['id']; ?>">Edit</a><br>
                                                <a href="#" class="ajax_view" id_keyword="<?php echo $list1['description']; ?>">View</a><br>
                                                <a href="#">Delete</a>
                                            </td>
                                        <?php else : ?>
                                            <td><a href="#">View</a><br></td>
                                        <?php
                                        endif;
                                    endif;
                                    ?>
                                </tr> 
                            <?php endforeach; ?>
                        </table></div>
                    <br>
                    <script type="text/javascript">
                        $(document).ready(function() {
                            $(".ajax_view").click(function() {
                                id_keyword = $(this).attr('id_keyword');
                                url = 'ajax.php?id_keyword=' + id_keyword;
                                $.ajax({
                                    url: url,
                                }).done(function(data) {
                                    $('.ajax_show').html(data);
                                });
                            });
                        });
                    </script>



                    </body>
                    </html>